package com.hsck.order.kds.setup.fragment.dyzd

import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.hjq.toast.Toaster
import com.hsck.order.kds.base.bean.CategoryBean
import com.hsck.order.kds.base.isnoam.network.livedata.LiveDataBus
import com.hsck.order.kds.base.isnoam.view.BaseFragment
import com.hsck.order.kds.base.utils.setOnSingleClickListener
import com.hsck.order.kds.databinding.FragmentAddDyzdBinding
import com.hsck.order.kds.setup.SetupActivity
import com.hsck.order.kds.setup.fragment.cxzd.GldkFragment
import kotlinx.android.synthetic.main.common_head_black.*

/**
 * 添加修改打印站点
 */
class AddDyzdFragment : BaseFragment<FragmentAddDyzdBinding>(),
    SetupActivity.UpdatableFragment {

    var type = ""
    var topFragment: Fragment? = null

    override fun inflateBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentAddDyzdBinding {
        return FragmentAddDyzdBinding.inflate(inflater)
    }

    fun setBack() {
        if (topFragment != null) {
            val activity = requireActivity() as SetupActivity
            activity.switchFragment(topFragment!!)
        }
    }

    override fun loadDataOnce() {
        super.loadDataOnce()
        lin_head_black_back.setOnClickListener {
            setBack()
        }
        if (type == "Add") {
            tv_head_black_title.text = "添加打印站点"
        } else {
            tv_head_black_title.text = "修改打印站点"
            binding.btnAddDyzdDel.visibility = View.VISIBLE
        }

        //获取打印机的传值
        LiveDataBus.with<ArrayList<CategoryBean>>("GldyjFragment").observe(this) {
            var name = ""
            it.forEach {
                name = if (name == "") {
                    it.tagName
                } else {
                    "$name,${it.tagName}"
                }
            }
            binding.tvAddDyzdNameGldk.text = "$name"
        }
        //获取关联档口的传值
        LiveDataBus.with<ArrayList<CategoryBean>>("GldkFragment").observe(this) {
            var name = ""
            it.forEach {
                name = if (name == "") {
                    it.tagName
                } else {
                    "$name,${it.tagName}"
                }
            }
            binding.tvAddDyzdNameGldk.text = "$name"
        }

        binding.apply {
            //关联打印机
            tvAddDyzdNameGldk.setOnSingleClickListener {
                var gldyjFragment = GldyjFragment()//关联档口
                val activity = requireActivity() as SetupActivity
                gldyjFragment.type = "Edit"
                gldyjFragment.topFragment = this@AddDyzdFragment
                activity.switchFragment(gldyjFragment)
            }
            //关联档口
            tvAddDyzdNameGldk.setOnSingleClickListener {
                var gldkFragment = GldkFragment()//关联档口
                val activity = requireActivity() as SetupActivity
                gldkFragment.type = "Edit"
                gldkFragment.topFragment = this@AddDyzdFragment
                activity.switchFragment(gldkFragment)
            }

            //删除
            btnAddDyzdDel.setOnClickListener {
                setBack()
            }
            //保存
            btnAddDyzdSave.setOnClickListener {
                if (checkInput()) {
                    setBack()
                }
            }
        }
    }

    override fun onFragmentVisible() {
        //可见
        if (_binding != null) {

        }
    }

    override fun onFragmentInvisible() {
        //不可见

    }

    /**
     * 检查输入数据是否合法
     */
    private fun checkInput(): Boolean {
        when {
            binding.edtAddDyzdName.text.toString().trim() == "" -> {
                Toaster.show("请输入打印站点名称")
                return false
            }
        }
        return true
    }

}